package Bio::Rambam::Proteomics::Searches::MYRIMATCH_search;
use Moose;
use namespace::autoclean;
with 'Bio::Rambam::Proteomics::Tools::ENGINE_search';
use Bio::Rambam::Proteomics qw(%ENZYMES %MODS $TEMPLATES);
use Data::Dumper;
use File::Basename;
use IPC::System::Simple qw(capture);
use Try::Tiny;
#use IO::Null;

has 'search'	=>	(is=>'rw',	isa=>'Bio::Rambam::Proteomics::Search',	default=>sub{{}}, trigger =>\&_start_myrimatch);

sub _start_myrimatch{
	my $self=shift;
	my $search=$self->search;
	###	Paths
	my $myrimatch_path=$search->engines->myrimatch;
	die "Myrimatch path is not correct.\n You should use the complete path to the executable file.\n" unless -B$myrimatch_path;
	my $myrimatch_out_folder=$search->folder."/myrimatch";	#omly works for linux
	my $myrimatch_out_file = basename($search->input_file);
	$myrimatch_out_file =$myrimatch_out_folder.'/'.$myrimatch_out_file;
	$myrimatch_out_file=~s/\.mgf/\.pepXML/;
	mkdir $myrimatch_out_folder unless -d$myrimatch_out_folder;
	my $myri_conf_file=$myrimatch_out_folder.'/'."myri_conf.txt";
	
	############################################################	Myrimatch Arguments
	my %replacements;
	$replacements{peptide_tol}=$search->peptide_tol;
	$replacements{pep_tol_units}='daltons' if $search->pep_tol_units eq 'Da';
	$replacements{pep_tol_units}='ppm' if $search->pep_tol_units eq 'ppm';
	$replacements{fragment_tol}=$search->fragment_tol;
	$replacements{database}=$search->database;
	$replacements{top_peptide_charge}=$search->top_peptide_charge;
	$replacements{enzyme}=$ENZYMES{$search->enzyme}{'Myrimatch'};
	$replacements{fragmentation}='manual:b,y' 	 if $search->fragmentation eq 'CID';
	$replacements{fragmentation}='manual:c,y,z*' if $search->fragmentation eq 'ETD';
	$replacements{fragmentation}='manual:c,y,z*' if $search->fragmentation eq 'ECD';
	$replacements{miss_cleavages}=$search->miss_cleavages;
	
	################		 Modifications
	my $fixed_mods="";
	foreach(split /\,/,$search->fixed_mods){
		my ($mod,$res)=split /\@/;
		$fixed_mods.=$res if length($res)==1;
		$fixed_mods.="(" if $res eq 'Nt';
		$fixed_mods.=")" if $res eq 'Ct';
		$fixed_mods.=" ".$MODS{$mod}{'mass'}." ";
	}
	$replacements{fixed_mods}=$fixed_mods;
	
	my $variable_mods="";
	my @symbols=qw(* & ^ % $ £ @ ?);
	foreach(split /\,/,$search->variable_mods){
		my ($mod,$res)=split /\@/;
		$variable_mods.=$res if length($res)==1;
		$variable_mods.="(" if $res eq 'Nt';
		$variable_mods.=")" if $res eq 'Ct';
		$variable_mods.=" ".(shift @symbols)." ";
		$variable_mods.=$MODS{$mod}{'mass'}." ";
	}
	$replacements{variable_mods}=$variable_mods;
	
	################
	
	my $myri_conf_template=$TEMPLATES."myrimatch.txt";
	my $myri_conf_text=template($myri_conf_template,\%replacements);
	open my $output,">",$myri_conf_file;
	print $output $myri_conf_text;
	close $output;
	
	my $command="$myrimatch_path -cfg $myri_conf_file -workdir $myrimatch_out_folder ";
	$command.="-cpus ".$search->threads unless $search->threads==0;
	$command.=" ".$search->input_file;
	run_command($command,$self);
	$self->output_file($myrimatch_out_file);
}








__PACKAGE__->meta->make_immutable;